package com.hnucm.cooklife.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.BaseActivity;
import com.hnucm.cooklife.base.Utils;
import com.hnucm.cooklife.bean.db.OrderDetail;
import com.hnucm.cooklife.bean.db.OrderSec;
import com.hnucm.cooklife.bean.db.Orderpic;
import com.hnucm.cooklife.contract.OrderDetaildetail.Contract;
import com.hnucm.cooklife.presenter.order.OrderDetailPresenter;
import com.hnucm.cooklife.presenter.order.OrderPresenter;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity<Contract.IOrderDetailView, OrderDetailPresenter> implements Contract.IOrderDetailView {

    private Context mContext;
    private List<String> banner = new ArrayList<>();

    @BindView(R.id.banner)
    Banner mBanner;

    @BindView(R.id.order_name)
    TextView mOrderName;

    @BindView(R.id.order_cap)
    TextView mOrderCap;

    @BindView(R.id.order_price)
    TextView mOrderPrice;

    @BindView(R.id.order_sale)
    TextView mOrderSale;

    @BindView(R.id.order_kefu)
    LinearLayout mKefu;


    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @BindView(R.id.order_share)
    LinearLayout mCar;

    @BindView(R.id.addToCar)
    LinearLayout mToCar;

    @BindView(R.id.addToCheche)
    TextView mAddToCheChe;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_orderdetail;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mContext = getApplicationContext();
        int oId = getIntent().getIntExtra("oId", 0);
//        banner.add(	"https://i2.chuimg.com/a787b63bb79245f09318c7f759a6f9d7_960w_1280h.jpg?imageView2/2/w/660/interlace/1/q/90");
//        banner.add("https://i2.chuimg.com/8e9d90bf79d7488aa9cbc9d24d440d32_1242w_699h.jpg?imageView2/2/w/660/interlace/1/q/90");
//        initBanner();
        initToolBar();
        mPresenter.loadOrderDetail(oId);
    }

    private void initToolBar() {
        mToolbar.setBackgroundColor(Color.TRANSPARENT);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initBanner() {
        mBanner.setAdapter(new BannerImageAdapter<String>(banner) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.itemView)
                        .load(data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(holder.imageView);
            }
        });
    }

    @Override
    protected OrderDetailPresenter createPresenter() {
        return new OrderDetailPresenter();
    }

    @Override
    public void onLoadOrderDetail(OrderDetail OrderDetailList) {
        List<Orderpic> orderpicList = OrderDetailList.orderpicList;
        for (int i = 0; i < orderpicList.size(); i++) {
            Orderpic orderpic = orderpicList.get(i);
            banner.add(orderpic.pic);
        }
        initBanner();

        mOrderName.setText(OrderDetailList.oname);
        mOrderCap.setText(OrderDetailList.ocap);

        String format = String.format(mContext.getResources().getString(R.string.order_price), OrderDetailList.oprice);
        mOrderPrice.setText(format);

        String sale = String.format(mContext.getResources().getString(R.string.order_sale), OrderDetailList.osold);
        mOrderSale.setText(sale);
//
    }

    @OnClick(R.id.order_share)
    public void click2(){
        Intent intent = new Intent(this, ShopActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @OnClick(R.id.order_kefu)
    public void click(){
        Intent intent = new Intent(this, ChatActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.addToCar)
    public void click3(){

    }


    @Override
    public void onRefreshOrderDetail(OrderDetail OrderDetailList) {

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoadingFailed() {

    }

    @Override
    public void onLoadSuccess() {

    }


}
